package poradnik.medyczny.Model;

import java.util.ArrayList;

import poradnik.medyczny.DB.DBAdapter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AdviceHistory {
	long id = 0;
	long advice_id;
	long symptom_id;
	long disease_id;
	long localization_id;
	boolean if_success;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getAdvice_id() {
		return advice_id;
	}

	public void setAdvice_id(long advice_id) {
		this.advice_id = advice_id;
	}

	public long getSymptom_id() {
		return symptom_id;
	}

	public void setSymptom_id(long symptom_id) {
		this.symptom_id = symptom_id;
	}

	public long getDisease_id() {
		return disease_id;
	}

	public void setDisease_id(long disease_id) {
		this.disease_id = disease_id;
	}

	public long getLocalization_id() {
		return localization_id;
	}

	public void setLocalization_id(long localization_id) {
		this.localization_id = localization_id;
	}

	public boolean isIf_success() {
		return if_success;
	}

	public void setIf_success(boolean if_success) {
		this.if_success = if_success;
	}

	public boolean Save(Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();

		ContentValues values = new ContentValues();
		values.put(DBAdapter.ADVICES_HISTOIRES_ADVICE_ID, advice_id);
		values.put(DBAdapter.ADVICES_HISTOIRES_LOCALIZATION_ID, localization_id);
		values.put(DBAdapter.ADVICES_HISTOIRES_SYMPTOM_ID, symptom_id);
		values.put(DBAdapter.ADVICES_HISTOIRES_DISEASE_ID, disease_id);
		values.put(DBAdapter.ADVICES_HISTOIRES_IF_SUCCESS, if_success ? 0 : 1);
		boolean successful = false;
		if (id != 0)
			successful = medical_db.update(DBAdapter.TABLE_ADVICES_HISTOIRES, values, DBAdapter.ADVICES_HISTOIRES_ID + "='" + id + "'", null);
		if (!successful) {
			id = medical_db.insert(DBAdapter.TABLE_ADVICES_HISTOIRES, values);
			successful = id == 0 ? false : true;

		}
		medical_db.close();

		return successful;
	}

	public AdviceHistory(long id, Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getAdviceHisotry(id);
		if (cursor != null)
			if (cursor.moveToFirst()) {
				advice_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_ADVICE_ID));
				this.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_ID));
				localization_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_LOCALIZATION_ID));
				symptom_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_SYMPTOM_ID));
				disease_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_DISEASE_ID));
				if_success = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_IF_SUCCESS)) == 0;
			}
		medical_db.close();
	}

	public AdviceHistory() {
	}

	public String getURL(boolean success) {
		String temp = "http://sonykuba.lh.pl/gps/updatedata.php?";
		if (symptom_id == 0) {
			temp += "table_name=LocalizationDiseaseLikehoods&disease_id=" + disease_id + "&localization_id=" + localization_id;
		} else if (disease_id == 0) {
			temp += "table_name=SymptomLocalizationLikehoods&symptom_id=" + symptom_id + "&localization_id=" + localization_id;
		} else if (localization_id == 0) {
			temp += "table_name=SymptomDiseaseLikehoods&disease_id=" + disease_id + "&symptom_id=" + symptom_id;
		}
		if (success && if_success)
			temp += "&success=0";
		else if (success && !if_success || !success && if_success)
			temp += "&success=1";
		else
			temp = "";
		return temp;

	}

	public static ArrayList<AdviceHistory> getAdviceHistory(long adviceId, Context context) {
		ArrayList<AdviceHistory> list = new ArrayList<AdviceHistory>();
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getAdviseHistoryForAdvise(adviceId);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				AdviceHistory loc = new AdviceHistory();
				loc.advice_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_ADVICE_ID));
				loc.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_ID));
				loc.localization_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_LOCALIZATION_ID));
				loc.symptom_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_SYMPTOM_ID));
				loc.disease_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_DISEASE_ID));
				loc.if_success = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_HISTOIRES_IF_SUCCESS)) == 0;

				list.add(loc);
			}
			cursor.close();
		}
		medical_db.close();
		return list;
	}
}
